package kaikeba.controller;


import kaikeba.bean.BootStrapTableCourier;
import kaikeba.bean.Courier;
import kaikeba.bean.Message;
import kaikeba.bean.ResultData;
import kaikeba.mvc.ResponseBody;
import kaikeba.service.CourierService;
import kaikeba.util.DateFormatUtil;
import kaikeba.util.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/***
 * 用户控制
 */
public class CourierController {

    @ResponseBody(LJ.XM_LU_JING+"/courier/console.do")
    public String console(HttpServletRequest request, HttpServletResponse response){
        Map<String, Integer> data = CourierService.console();
        System.out.println("console.do获取的date"+data);
        Message msg = new Message();
        if(data.size() == 0){
            msg.setStatus(-1);
        }else {
            msg.setStatus(0);
        }
        msg.setData(data);
        String json = JSONUtil.toJSON(msg);
        System.out.println("console的JSON"+json);
        return json;
    }


    @ResponseBody(LJ.XM_LU_JING+"/courier/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response){
        //1.    获取查询数据的起始索引值
        int offset = Integer.parseInt(request.getParameter("offset"));
        //2.    获取当前页要查询的数据量
        int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        //3.    进行查询
        List<Courier> list = CourierService.findAll(true, offset, pageNumber);
        List<BootStrapTableCourier> list2 = new ArrayList<>();
       for(Courier c:list){
            String inTime = c.getInTime()==null? "未曾登录":DateFormatUtil.format(c.getInTime());
            String loginTime = c.getLoginTime()==null?"未登记的黑户":DateFormatUtil.format(c.getLoginTime());
            String status = c.getStatus()==0?"未使用":"使用中";
           BootStrapTableCourier c2 = new BootStrapTableCourier(c.getId(),c.getNumber(),c.getUsername(),c.getUserPhone(),c.getPassword(),loginTime,inTime,status);
            list2.add(c2);
        }
        Map<String, Integer> console = CourierService.console();
        Integer total = console.get("courier_total");
        //4.    将集合封装为 bootstrap-table识别的格式
        ResultData<BootStrapTableCourier> data = new ResultData<>();
        data.setRows(list2);
        data.setTotal(total);
        String json = JSONUtil.toJSON(data);
        return json;
    }

    @ResponseBody(LJ.XM_LU_JING+"/courier/insert.do")
    public String insert(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        String userPhone = request.getParameter("userPhone");
        String number = request.getParameter("number");
        String password = request.getParameter("password");
        //user e = new user(number,company,username,userPhone, UserUtil.getUserPhone(request.getSession()));
        Date loginTime = new Date();
        Courier e = new Courier(number,username,userPhone,password);
        boolean flag = CourierService.insert(e);
        Message msg = new Message();
        if(flag){
            //录入成功
            msg.setStatus(0);
            msg.setResult("用户加入成功!");
        }else{
            //录入失败
            msg.setStatus(-1);
            msg.setResult("用户加入失败!");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody(LJ.XM_LU_JING+"/courier/find.do")
    public String find(HttpServletRequest request,HttpServletResponse response){
        String userPhone = request.getParameter("userPhone");
        System.out.println("进入用户号查找用户信息userPhone："+userPhone);
        Courier c = CourierService.findByPhone(userPhone);
        Message msg = new Message();
        if(c == null){
            msg.setStatus(-1);
            msg.setResult("用户不存在");
        }else{
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(c);
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody(LJ.XM_LU_JING+"/courier/update.do")
    public String update(HttpServletRequest request,HttpServletResponse response){
        int id = Integer.parseInt(request.getParameter("id"));
        String number = request.getParameter("number");
        String password = request.getParameter("password");
        String username = request.getParameter("username");
        String userPhone = request.getParameter("userPhone");
        Courier newCourier = new Courier(number,username,userPhone,password);
        System.out.println("进入courier控制的newCourier"+newCourier);
        boolean flag = CourierService.update(id, newCourier);
        Message msg = new Message();
        if(flag){
            msg.setStatus(0);
            msg.setResult("修改成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("修改失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody(LJ.XM_LU_JING+"/courier/delete.do")
    public String delete(HttpServletRequest request,HttpServletResponse response){
        int id = Integer.parseInt(request.getParameter("id"));
        boolean flag = CourierService.delete(id);
        Message msg = new Message();
        if(flag){
            msg.setStatus(0);
            msg.setResult("删除成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("删除失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }
}
